package com.jwgf.smarowindingmachine_unity.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jwgf.common.ViewCache;
import com.jwgf.fram.FramManager;
import com.jwgf.fram.FramManager.OnFramChangedListener;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;

import java.util.ArrayList;


public class MachineStateDofferStateFragment extends BaseFragment implements OnFramChangedListener{
	public static final String fragmentTag = "MachineStateDofferStateFragment";
	public GifSurfaceView gifContainer;
	private View v;
	private int plc6Error_d162 = 0;
	private int plc6Error_d163 = 0;
	
	private int[][] errorArray162 = {{0x01,R.drawable.doffer_emergency_stop},//急停按钮按下
			  {0x02,R.drawable.doffer_tube_empty},//没有空管
			  {0x04,R.drawable.doffer_package_motor_fault},//筒纱排除杆未回到原点
			  {0x08,R.drawable.doffer_rotary_solenoid},//旋转电磁铁打不开
			  {0x10,R.drawable.doffer_strike_limit},//小车触发极限位置
			  {0x20,R.drawable.doffer_package_sensor_fault},//筒纱提起传感器没检测到原点
			  {0x40,R.drawable.doffing_time_20s},//落筒超时
			  {0x80,R.drawable.doffer_package_on_conveyer_belt},//传送带上有筒纱不能落筒
			  {0x100,R.drawable.doffer_clutch_lock},//离合器脱开
			  {0x200,R.drawable.doffer_difficult_work},//行走遇阻
			  {0x400,R.drawable.doffer_second_overtime},//第二次落筒循环仍超时
			  {0x800,R.drawable.doffer_no_equal_read},//计算磁钢数与读出单锭码不相同
			  {0x1000,R.drawable.doffer_failed_read},//读码失败
			  {0x2000,R.drawable.doffer_origin_sensor_fault},//循环轴电机原点传感器没检测到原点
			  {0x4000,R.drawable.doffer_yarn_hook_fault},//纱线钩未检测到原点
			  {0x8000,R.drawable.doffer_arm_origin_sensor_fault},//支臂原点传感器没检测到原点
			  {0x10000,R.drawable.doffer_not_receive_stop_signal},//小车收到上位机的停车信号
			  {0x20000,R.drawable.doffer_hit_limit},//小车频繁撞击极限
			  {0x40000,R.drawable.doffer_solenoid_detected},//电磁铁原点传感器检测到信号
			  {0x80000,R.drawable.doffer_not_doffing}//筒纱输送开，小车不落筒
			};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		v = ViewCache.getInstance().getView(R.layout.fragment_machine_state_doffer_state);
		refreshError();
		FramManager.getInstance().registerOnFramChangedListener(this);
		return v;
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if (hidden) {
			gifContainer.setVisibility(View.INVISIBLE);
		} else {
			refreshError();
			gifContainer.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onFramChanged(FramManager framManager, String key) {
//		if (key.equals(String.valueOf(FxUtils.getkey(6, 162)))) {
//			plc6Error_d162 = framManager.getInt(key, 0);
//			refreshError();
//		} else if (key.equals(String.valueOf(FxUtils.getkey(6, 163)))) {
//			plc6Error_d163 = framManager.getInt(key, 0);
//			
//		}
	}
	
	public void refreshError()
	{
		if (MachineParameterSharedPreference.getInstance().getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("3")) {
			ImageView imageView = (ImageView) v.findViewById(R.id.doffer_status_bk);
			imageView.setImageResource(R.drawable.doffer_error_bk);
		} else if (MachineParameterSharedPreference.getInstance().getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("5")) {
			ImageView imageView = (ImageView) v.findViewById(R.id.doffer_status_bk);
			imageView.setImageResource(R.drawable.doffer_error_bk);
		}
		gifContainer = (GifSurfaceView) v.findViewById(R.id.gif_container);
		ArrayList<Integer> list = new ArrayList<Integer>();
		if(plc6Error_d162!=0)
		{
			for(int i=0;i<errorArray162.length;i++)
			{
				if((errorArray162[i][0] & plc6Error_d162)!=0)
				{
					list.add(errorArray162[i][1]);
				}
			}
		}
		gifContainer.setMovieList(list);
	}
	
	
}
